<?php

/**
 * @name AddressController
 * @desc 控制器
 */
class AddressController extends UserCommonController
{
    //地址列表
    public function listAction(){

        $params = $this->post();

        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;

        $userAddressModel = UserAddressModel::getSingleton();
        $list = $userAddressModel->db
            ->field('id, user_name, moble, areas, address, create_time, is_default')
            ->where(['user_id'=> $this->user_info['user_id'], 'is_delete'=> 0])
            ->order('id asc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $userAddressModel->db
            ->where(['user_id'=> $this->user_info['user_id'], 'is_delete'=> 0])
            ->count();

        foreach($list as &$val){
            $val['moble'] = \Vendor\Kstring::hideMobile($val['moble']);
        }

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page),
            'pageNum'   => ceil($total/$row)
        ];

        $this->ajaxReturn(0, '', $data);
    }

    //详情
    public function detailAction(){
        $params = $this->post();

        if(!isset($params['id'])){
            $this->ajaxReturn(400, '未获取到数据ID');
        }

        $userAddressModel = UserAddressModel::getSingleton();
        $detail = $userAddressModel->find(['id'=> $params['id'], 'user_id'=> $this->user_info['user_id'], 'is_delete'=> 0]);

        if(!$detail){
            $this->ajaxReturn(400, '数据不存在');
        }

        $this->ajaxReturn(0, 'success', $detail);
    }

    //添加地址
    public function addAction(){
        $params = $this->post();

        $rule = [
            'user_name'     => 'require|length:1,10',
            'moble' => 'require|regex:moble',
            'areas' => 'require|length:1,255',
            'address' => 'require|length:1,255',
            'is_default' => 'in:1,0',
        ];
        $msg = [
            'user_name.require' => '必须填写收货人',
            'user_name.length'   => '收货人名称必须1~10个字符',
            'moble.require' => '必须输入联系手机号码',
            'moble.regex'   => '手机号码不正确',
            'areas.require' => '必须选择区域',
            'address.require'   => '必须输入详细地址',
            'address.length'   => '详细地址必为1~200个字符',
            'is_default.in'   => '是否默认指定不正确',
        ];
        $validate = new \Vendor\Kvalidate($rule,$msg);
        if(!$validate->check($params)){
            $this->ajaxReturn(400, $validate->getError());
        }

        $params['user_id'] = $this->user_info['user_id'];
        $userAddressModel = UserAddressModel::getSingleton();

        $id = $userAddressModel->add($params);
        if(!$id){
            $this->ajaxReturn(400, '添加失败');
        }

        if(isset($params['is_default']) && $params['is_default'] == 1){
            $userAddressModel->update(['id#!='=> $id, 'user_id'=> $this->user_info['user_id']], ['is_default'=> 0, 'update_time'=> time()]);
        }

        //检查第一张默认
        $defaultList = $userAddressModel->find(['user_id'=> $this->user_info['user_id'], 'is_default'=> 1, 'is_delete'=> 0]);
        if(!$defaultList){
            $userAddressModel->update(['id'=> $id], ['is_default'=> 1]);
        }

        $this->ajaxReturn(0, 'seccess');
    }

    //修改地址
    public function editAction(){
        $params = $this->post();

        $rule = [
            'id'    => 'require|integer',
            'user_name'     => 'length:1,10',
            'moble' => 'regex:moble',
            'areas' => 'length:1,255',
            'address' => 'length:1,255',
            'is_default' => 'in:1,0',
        ];
        $msg = [
            'id.require'    => '未获取数据ID',
            'id.integer'    => 'ID不正确',
            'user_name.length'   => '收货人名称必须1~10个字符',
            'moble.regex'   => '手机号码不正确',
            'address.length'   => '详细地址必为1~200个字符',
            'is_default.in'   => '是否默认指定不正确',
        ];
        $validate = new \Vendor\Kvalidate($rule,$msg);
        if(!$validate->check($params)){
            $this->ajaxReturn(400, $validate->getError());
        }

        $cond = [
            'user_id'   => $this->user_info['user_id'],
            'id'    => $params['id'],
        ];
        unset($params['id']);
        $params['update_time'] = time();
        $userAddressModel = UserAddressModel::getSingleton();
        $res = $userAddressModel->update($cond, $params);
        if(!$res){
            $this->ajaxReturn(400, '修改失败');
        }

        if(isset($params['is_default']) && $params['is_default'] == 1){
            $userAddressModel->update(['user_id'=> $this->user_info['user_id'], 'id#not in'=> $cond['id']], ['is_default'=> 0, 'update_time'=> time()]);
        }

        $this->ajaxReturn(0, 'seccess');
    }

    //设置默认
    public function setdefAction(){
        $params = $this->post();
        if(!isset($params['id']) || !$params['id']){
            $this->ajaxReturn(400, '必须提交数据ID');
        }

        $userAddressModel = UserAddressModel::getSingleton();
        if(!$userAddressModel->update(['user_id'=> $this->user_info['user_id']], ['is_default'=> 0, 'update_time'=> time()])){
            $this->ajaxReturn(400, '设置失败');
        }
        if(!$userAddressModel->update(['user_id'=> $this->user_info['user_id'],'id'=> $params['id']], ['is_default'=> 1, 'update_time'=> time()])){
            $this->ajaxReturn(400, '设置失败.');
        }

        $this->ajaxReturn(0, '设置成功');
    }

    //获取默认地址
    public function defAction(){
        $userAddressModel = UserAddressModel::getSingleton();
        $default = $userAddressModel->db->where(['user_id'=> $this->user_info['user_id'], 'is_delete'=> 0])->order('is_default desc')->find();

        $this->ajaxReturn(0, 'success', $default?$default: '');
    }

    //删除
    public function delAction(){
        $params = $this->post();
        if(!isset($params['id']) || !$params['id']){
            $this->ajaxReturn(400, '未获取到ID');
        }

        $userAddressModel = UserAddressModel::getSingleton();
        if(!$userAddressModel->update(['user_id'=> $this->user_info['user_id'], 'id'=> $params['id']], ['is_delete'=> 1, 'update_time'=> time()])){
            $this->ajaxReturn(400, '操作失败');
        }

        $this->ajaxReturn(0, 'success');
    }

}
